package com.example.recordingpen_datacollection.Utils.hy;

import com.example.recordingpen_datacollection.Utils.HTTPJSON;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.google.gson.Gson;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class AIUtil {
    private static final String SIGN_HOST = "hunyuan.tencentcloudapi.com";
    private static final String URL = "https://hunyuan.tencentcloudapi.com";
    private static final long appId = 1302736335L;
    private static final String secretId = "AKIDwdMp55jvkIbfU3flzxdFd2B7mBRShEH5";
    private static final String secretKey = "gngYBXdU9Z0DI70rRloqcssbKmy2tU4w";

    public static String staAianswer(String role ,String content,String action ) {

        HyV2Request hyV2Request = new HyV2Request();
        hyV2Request.setTopP(0.2F);
        hyV2Request.setTemperature(0.1F);
//            MessageV2 message2 = new MessageV2("system","你是一个会议总结助手");message2
            MessageV2 message = new MessageV2(role,content);
            MessageV2[] messages = new MessageV2[]{message};
        hyV2Request.setMessagesV2(messages);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(hyV2Request);
        System.out.println(jsonStr);


        HyV2Service hyV2Service = new HyV2Service(appId, secretId, secretKey,"ChatStd");
        String chatStd = null;
        long millis = System.currentTimeMillis()/ 1000;
        try {
            chatStd = hyV2Service.sign("ChatStd", String.valueOf(millis),jsonStr);
            System.out.println(chatStd +  "  chatStd");
        } catch (Exception e) {
            e.printStackTrace();
        }

        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(3L, TimeUnit.MINUTES)   //  连接超时时间
                .readTimeout(3L, TimeUnit.MINUTES)      //  读取超时时间
                .writeTimeout(3L, TimeUnit.MINUTES)     //  写入超时时间
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(jsonStr,mediaType);
        Request request = new Request.Builder()
                .url(URL)
                .method("POST", body)
                .addHeader("X-TC-Action", "ChatStd")
                .addHeader("Content-Type", "application/json")
                .addHeader("Host", SIGN_HOST)
                .addHeader("Authorization", chatStd)
                .addHeader("X-TC-Timestamp", millis+"")
                .addHeader("X-TC-Version", "2023-09-01")
                .addHeader("X-TC-Region", "ap-guangzhou")

                .build();
        Response response = null;
        String json = null;
        try {
            response = client.newCall(request).execute();
            json = HTTPJSON.json(response.body().string());
        } catch (IOException e) {
            throw new JWException(500,e.toString());
        }
        System.out.println(json);
        return json;
    }
}
